$(function() {
	var sn = $(".inHouse_menu .menu_left #sn");
	var up_computer = $(".inHouse_menu .menu_left #up_computer");
	var up_plate = $(".inHouse_menu .menu_left #up_plate");
	var down_plate = $(".inHouse_menu .menu_left #down_plate");
	var navigation = $(".inHouse_menu .menu_left #navigation");
	var radar = $(".inHouse_menu .menu_right #radar");
	var walk_left = $(".inHouse_menu .menu_right #walk_driven_left");
	var walk_right = $(".inHouse_menu .menu_right #walk_driven_right");
	var dlp = $(".inHouse_menu .menu_right #dlp");
	var aiui = $(".inHouse_menu .menu_right #aiui");

	getFocus(sn);

	var eleValue = new Array();
	sn.bind('input propertychange', function() {
		if (sn.val().length == 12) {
			up_computer.val("");
			eleValue[0] = sn.val();
			getFocus($(".inHouse_menu .menu_left #up_computer"));

		}
	});
	up_computer.bind('input propertychange', function() {
		if (up_computer.val().length == 12) {
			eleValue[1] = up_computer.val();
			up_plate.val("");
			getFocus(up_plate);

		}
	});
	up_plate.bind('input propertychange', function() {
		if (up_plate.val().length == 12) {
			down_plate.val("");
			getFocus(down_plate);
			eleValue[2] = up_plate.val();
		}
	});
	down_plate.bind('input propertychange', function() {
		if (down_plate.val().length == 12) {
			navigation.val("");
			getFocus(navigation);
			eleValue[3] = down_plate.val();
		}
	});
	navigation.bind('input propertychange', function() {
		if (navigation.val().length == 12) {
			radar.val("");
			getFocus(radar);
			eleValue[4] = navigation.val();
		}
	});
	radar.bind('input propertychange', function() {
		if (radar.val().length == 12) {
			walk_left.val("");
			getFocus(walk_left);
			eleValue[5] = radar.val();
		}
	});
	walk_left.bind('input propertychange', function() {
		if (walk_left.val().length == 12) {
			walk_right.val("");
			getFocus(walk_right);
			eleValue[6] = walk_left.val();
		}
	});
	walk_right.bind('input propertychange', function() {
		if (walk_right.val().length == 12) {
			dlp.val("");
			getFocus(dlp);
			eleValue[7] = walk_right.val();
		}
	});
	dlp.bind('input propertychange', function() {
		if (dlp.val().length == 12) {
			aiui.val("");
			getFocus(aiui);
			eleValue[8] = dlp.val();
		}
	});
	aiui.bind('input propertychange', function() {
		if (aiui.val().length == 12) {
			eleValue[9] = aiui.val();
		}
	});

	// 获得焦点事件
	function getFocus(item) {
		item.focus();
	}

	$(".inHouse_menu .save #sub").click(function() {
		if (judgeArr()) {
			var data = {
				sn : eleValue[0],
				up_computer : eleValue[1],
				up_plate : eleValue[2],
				down_plate : eleValue[3],
				navigation : eleValue[4],
				radar : eleValue[5],
				walk_driven_left : eleValue[6],
				walk_driven_right : eleValue[7],
				dlp : eleValue[8],
				aiui : eleValue[9],
				robot_type : "GREET_ROBOT",
				robot_version : "all"
			}

			$.ajax({
				url : "./toHouse",
				type : "POST",
				data : JSON.stringify(data), // 转JSON字符串
				dataType : 'json',
				contentType : 'application/json', // contentType很重要
				success : function(result) {
					if(result.status == 200){
						reloads();
						meg_append("sn:"+eleValue[0]+" 的机器人入库成功！","#00CC00");
					}else{
						meg_append(result.message,"red");
					}
				}
			});
		} else {
			reloads();
			meg_append("输入框参数有空值，请重新输入！","red");
		}

	});
	$(".inHouse_menu .save #clear_all").click(function() {
		reloads();
		
	});
	function judgeArr() {
		if (eleValue.length != 10) {
			return false;
		} else {
			for (var i = 0; i < eleValue.length; i++) {
				if (eleValue[i] == "" || eleValue[i] == null) {
					return false;
				}
			}
		}
		return true;
	}
	
	function reloads(){
		$(".inHouse_menu .menu_item input").val("");
		getFocus(sn);
	}
	function meg_append(meg,cor){
		var str = '<strong>提示：</strong><label style="color: '+cor+';">'+meg+'</label>';
		$(".inHouse_menu .meg").empty();
		$(".inHouse_menu .meg").append(str);
	}
	
	
});